package yja.gzw.app.fragmentview

import android.content.Intent
import android.view.MotionEvent
import android.widget.ImageView
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import yja.gzw.app.R
import yja.gzw.app.activity.FabaActivity
import yja.gzw.app.activity.JinpingshipingActivity
import yja.gzw.app.activity.SanyuanActivity
import yja.gzw.app.activity.SiyuanActivity
import yja.gzw.app.activity.TopshowActivity
import yja.gzw.app.base.BaseFragment
import yja.gzw.app.databinding.FragmentHomeBinding
import yja.gzw.app.viewmodel.Mainviewmodel
import com.google.android.material.tabs.TabLayout

class HomeFragment :BaseFragment<Mainviewmodel, FragmentHomeBinding>(R.layout.fragment_home){
    val list_fragment= mutableListOf<Fragment>(
        TuijianFragment(),GuanzhuFragment()
    )
    var listtlit= mutableListOf<String>("推荐","关注")
    val imageUrls = listOf(
        R.mipmap.longboti,
        R.mipmap.longboti,
        R.mipmap.longboti

    )
    override fun initView() {


        binding.shipinzhuanqu.setOnClickListener {
            val intent=Intent(context, JinpingshipingActivity::class.java)
            context?.startActivity(intent)


        }



        binding.xbn.setData(imageUrls,null)
        binding.xbn.setmAutoPalyTime(3000)
        binding.xbn.setmAutoPlayAble(true)
     binding.xbn.setmAdapter { banner, view, position ->

         Glide.with(banner.context).load(imageUrls[position]).into(view as ImageView)


     }

        var initialX = 0f
        var initialY = 0f
        var initialTouchX = 0f
        var initialTouchY = 0f

//        binding.fabuanniu.setOnTouchListener { v, event ->
//            when (event.action) {
//                MotionEvent.ACTION_DOWN -> {
//                    initialX = v.x
//                    initialY = v.y
//                    initialTouchX = event.rawX
//                    initialTouchY = event.rawY
//                    true
//                }
//                MotionEvent.ACTION_MOVE -> {
//                    v.x = initialX + (event.rawX - initialTouchX)
//                    v.y = initialY + (event.rawY - initialTouchY)
//                    true
//                }
//                else -> false
//            }
//        }

        binding.fabuanniu.setOnClickListener {
            val intent = Intent(requireContext(), FabaActivity::class.java)
            startActivity(intent)
        }


        binding.viewpager.adapter=object : FragmentPagerAdapter(childFragmentManager){
            override fun getCount(): Int {

                return list_fragment.size

            }

            override fun getItem(position: Int): Fragment {

                return list_fragment[position]

            }



        }
        binding.tabs.setupWithViewPager(binding.viewpager)

        binding.tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {


            }

            override fun onTabUnselected(tab: TabLayout.Tab) {


            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }


        })

        for (i in 0  until listtlit.size ){

            binding.tabs.getTabAt(i)?.setText(listtlit[i])



        }






        binding.sanyuanzhuanqu.setOnClickListener {

            var intent=Intent(context, SanyuanActivity::class.java)
            startActivity(intent)
        }

        binding.siyuanzhuanqu.setOnClickListener {

            var intent=Intent(context, SiyuanActivity::class.java)
            startActivity(intent)
        }




    }

    override fun dataObservese() {



    }
}